<?php
class municipio extends CI_Controller {
	var $titp='municipio';
	var $url ='municipio/';

	function index(){
		redirect($this->url.'filteredgrid');
	}

	function filteredgrid(){
		$this->load->library('rapyd');

		$filter = new datafilter_library();
		$filter->label = 'Municipios en los que funciona la mision del arbol en el Estado Merida';
		$filter->db->select('*');
		$filter->db->from('municipios');

		$filter->field('input','nombre'  ,'Nombre')->attributes(array('style' => 'width:170px'));
		//$filter->field('input','ubicacion'  ,'Ubicacion de la zona')->attributes(array('style' => 'width:170px'));

		$filter->buttons('reset', 'search');
		$filter->build();

		$uri = anchor($this->url.'dataedit/show/<raencode><#id#></raencode>','<#id#>');

		$grid = new datagrid_library();
		$grid->label = 'Lista de los Municipios';
		$grid->per_page = 40;
		$grid->cid = '';
		$grid->source($filter);

		$grid->column('nombre','Nombre del municipio',true)->url('municipio/dataedit/show/{id}');
		//$grid->column('id_estado','Identificador del municipio',true);
		
		$grid->add_button(array('url'=>$this->url.'dataedit/create'));
		$action = "javascript:window.location.href='".site_url('/')."'";
		$grid->button('btn_reg', 'Regresar', $action, 'TR');
		$grid->build();

		$data['content'] = $filter->output.$grid->output;
		$data['head']    = $this->rapyd->head().script('jquery.js');
		$data['title']   = '';
		$this->load->view('view_ventanas_sola', $data);

	}

	function dataedit(){
		$this->load->library('rapyd');

		$edit = new dataedit_library();
		$edit->label = 'Municipio';
		$edit->back_url = site_url($this->url.'filteredgrid');

		$edit->source('municipios');
		$edit->pre_process(array('delete'), array($this, 'pre_delete'));
	
		$edit->field('input','nombre','Nombre del municipio')->rule('trim|required|ucwords');
		//$edit->field('input','id_estado','Identificador del estado')->rule('trim|ucwords|required');

		$edit->buttons('modify','save','undo','delete','back');

		$edit->build();

		$data['head']    = $this->rapyd->head();
		$data['title']   = '<h1>Caracteristicas del municipio</h1>';
		$data['content'] = $edit;
		$this->load->view('view_ventanas_sola', $data);
	}

	function pre_delete($model){
		$id = $model->get('id');

		$this->db->from('parroquias');
		$this->db->where('id_municipios',$id);
		$cana = $this->db->count_all_results();

		if($cana==0){
			return true;
		}
		$model->error_string = '--No se puede eliminar un municipio con parroquias relacionadas.';
		return false;
	}

	
}

